using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
//Written By: Jason Deatherage August 2008 Phoenix, AZ Home of the blazing inferno.

namespace SlideShowGenerator
{
    public partial class FileRenameForm : Form
    {
        private bool nonNumberEntered = false; //Used to make sure only numbers are entered in the start numbers text box

        public FileRenameForm()
        {
            InitializeComponent();
        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            DialogResult result = FBDialogRename.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtSourceDirectory.Text = FBDialogRename.SelectedPath;

                if (chkSame.Checked == true)
                {
                    txtDestinationDirectory.Text = txtSourceDirectory.Text;
                    
                }
            }
        }//End source location browser click

        private void chkSame_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSame.Checked == true)
            {
                txtDestinationDirectory.Text = txtSourceDirectory.Text;
                txtDestinationDirectory.ReadOnly = true;
            }

            if (chkSame.Checked == false)
            {
                txtDestinationDirectory.ReadOnly = false;
                txtDestinationDirectory.Text = "";
            }
            
        }//End of checking if keep files in same directory is checked

        private void btnRename_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSourceDirectory.Text == "")
                {
                    MessageBox.Show("Please select a source directory.");
                }
                else if (txtDestinationDirectory.Text == "")
                {
                    MessageBox.Show("Please select a destination directory.");
                }
                else if (txtStartNumbersAt.Text == "")
                {
                    MessageBox.Show("Please enter a number to start counter.");
                }
                else
                {
                    pbFileRename.Visible = true;
                    pbFileRename.Value = 0;
                    pbFileRename.Maximum = Directory.GetFiles(txtSourceDirectory.Text).Length;

                    if (rbExistingName.Checked == true && cbPlaceNumber.SelectedItem.ToString() == "after")
                    {
                        MoveFilesAfterSameName();
                    }
                    if (rbExistingName.Checked == true && cbPlaceNumber.SelectedItem.ToString() == "before")
                    {
                        MoveFileBeforeSameName();
                    }
                    if (rbNewName.Checked == true && cbPlaceNumber.SelectedItem.ToString() == "before")
                    {
                        MoveFileBeforeNewName();
                    }
                    if (rbNewName.Checked == true && cbPlaceNumber.SelectedItem.ToString() == "after")
                    {
                        MoveFileAfterNewName();
                    }

                    
                }
            }
            catch
            {

            }
                 
        }


        private void cbSeparatedBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExampleFileName();
        }

        private void ExampleFileName()
        {
            try
            {

                String strFileName = "";

                if (rbExistingName.Checked == true)
                {
                    strFileName = strFileName + "Name";
                    //lblExample.Text = "Example:  Name.xxx";
                }
                if (rbNewName.Checked == true)
                {
                    strFileName = strFileName + txtNewName.Text;
                    //lblExample.Text = "Example:  " + txtNewName.Text + ".xxx";
                }

                //**********Checks if the number should be placed after************
                if (cbPlaceNumber.SelectedItem.ToString() == "after" && cbSeparatedBy.SelectedItem.ToString() == "space")
                {
                    strFileName = strFileName + " " + txtStartNumbersAt.Text;
                }
                else if (cbPlaceNumber.SelectedItem.ToString() == "after" && cbSeparatedBy.SelectedItem.ToString() == "underscore")
                {
                    strFileName = strFileName + "_" + txtStartNumbersAt.Text;
                }
                else if (cbPlaceNumber.SelectedItem.ToString() == "after" && cbSeparatedBy.SelectedItem.ToString() == "dash")
                {
                    strFileName = strFileName + "-" + txtStartNumbersAt.Text;
                }
                else if (cbPlaceNumber.SelectedItem.ToString() == "after" && cbSeparatedBy.SelectedItem.ToString() == "nothing")
                {
                    strFileName = strFileName + txtStartNumbersAt.Text;
                }

                //**********Checks if the number should be placed before************
                if (cbPlaceNumber.SelectedItem.ToString() == "before" && cbSeparatedBy.SelectedItem.ToString() == "space")
                {
                    strFileName = txtStartNumbersAt.Text + " " + strFileName;
                }
                else if (cbPlaceNumber.SelectedItem.ToString() == "before" && cbSeparatedBy.SelectedItem.ToString() == "underscore")
                {
                    strFileName = txtStartNumbersAt.Text + "_" + strFileName;
                }
                else if (cbPlaceNumber.SelectedItem.ToString() == "before" && cbSeparatedBy.SelectedItem.ToString() == "dash")
                {
                    strFileName = txtStartNumbersAt.Text + "-" + strFileName;
                }
                else if (cbPlaceNumber.SelectedItem.ToString() == "before" && cbSeparatedBy.SelectedItem.ToString() == "nothing")
                {
                    strFileName = txtStartNumbersAt.Text + strFileName;
                }

                strFileName = "Example:  " + strFileName + ".xxx";
                lblExample.Text = strFileName;

            }
            catch
            {

            }
        }//End of example file name

        private void rbExistingName_CheckedChanged(object sender, EventArgs e)
        {
            ExampleFileName();
        }

        private void rbNewName_CheckedChanged(object sender, EventArgs e)
        {
            ExampleFileName();
        }

        private void txtNewName_TextChanged(object sender, EventArgs e)
        {
            ExampleFileName();
        }

        private void cbPlaceNumber_SelectedIndexChanged(object sender, EventArgs e)
        {

            ExampleFileName();
        }

        private void FileRenameForm_Load(object sender, EventArgs e)
        {
            cbPlaceNumber.SelectedItem = "after";
            cbSeparatedBy.SelectedItem = "nothing";
        }//End selects default items on load

        private void txtStartNumbersAt_TextChanged(object sender, EventArgs e)
        {
            ExampleFileName();
        }

        private void btnDestination_Click(object sender, EventArgs e)
        {
            DialogResult result = FBDialogRename.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtDestinationDirectory.Text = FBDialogRename.SelectedPath;
            }
        }//End Destination location click

        private void MoveFilesAfterSameName()
        {
            try
            {
                int intFileSourceLength = txtSourceDirectory.Text.Length;

                int intFileCounter = Convert.ToInt32(txtStartNumbersAt.Text);
                int intNumFilesCopied = 0;
                string foldername = txtSourceDirectory.Text;

                    if (cbSeparatedBy.SelectedItem.ToString() == "space")
                    {
                        foreach (string f in Directory.GetFiles(foldername))
                        {
                            pbFileRename.Value++;
                            String strFileExt = GetFileExtension(f);
                            if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                            {
                                if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                                {
                                    File.Move(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + " " + intFileCounter + strFileExt);
                                }
                                else
                                {
                                    File.Copy(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + " " + intFileCounter + strFileExt);
                                }
                                //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + " " + intFileCounter + strFileExt);
                                intFileCounter++;
                                intNumFilesCopied++;
                            }
                        }
                    }
                    else if (cbSeparatedBy.SelectedItem.ToString() == "underscore")
                    {
                        foreach (string f in Directory.GetFiles(foldername))
                        {
                            pbFileRename.Value++;
                           String strFileExt = GetFileExtension(f);
                           if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                           {
                               if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                               {
                                   File.Move(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + "_" + intFileCounter + strFileExt);
                               }
                               else
                               {
                                   File.Copy(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + "_" + intFileCounter + strFileExt);
                               }
                               //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + "_" + intFileCounter + strFileExt);
                               intFileCounter++;
                               intNumFilesCopied++;
                           }
                        }
                    }
                    else if (cbSeparatedBy.SelectedItem.ToString() == "dash")
                    {
                        foreach (string f in Directory.GetFiles(foldername))
                        {
                            pbFileRename.Value++;
                            String strFileExt = GetFileExtension(f);
                            if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                            {
                                if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                                {
                                    File.Move(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + "-" + intFileCounter + strFileExt);
                                }
                                else
                                {
                                    File.Copy(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + "-" + intFileCounter + strFileExt);
                                }
                                //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + "-" + intFileCounter + strFileExt);
                                intFileCounter++;
                                intNumFilesCopied++;
                            }
                        }
                    }
                    else if (cbSeparatedBy.SelectedItem.ToString() == "nothing")
                    {
                        foreach (string f in Directory.GetFiles(foldername))
                        {
                            pbFileRename.Value++;
                            String strFileExt = GetFileExtension(f);
                            if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                            {
                                if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                                {
                                    File.Move(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + intFileCounter + strFileExt);
                                }
                                else
                                {
                                    File.Copy(f, txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + intFileCounter + strFileExt);
                                }
                                //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + f.Substring(intFileSourceLength, f.Length - intFileSourceLength - 4) + intFileCounter + strFileExt);
                                intFileCounter++;
                                intNumFilesCopied++;
                            }
                        }
                    }
                    MessageBox.Show(intNumFilesCopied + " files renamed.");
                    Close();
                }
                
            catch (Exception exName)
            {
                MessageBox.Show(exName.Message.ToString());
            }

        }//End of MoveFileSameNameAfter

        private void MoveFileBeforeSameName()
        {
            try
            {
                int intFileSourceLength = txtSourceDirectory.Text.Length;

                int intFileCounter = Convert.ToInt32(txtStartNumbersAt.Text);
                int intNumFilesCopied = 0;
                string foldername = txtSourceDirectory.Text;

                if (cbSeparatedBy.SelectedItem.ToString() == "space")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + " " + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + " " + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + " " + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "underscore")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "_" + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "_" + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + "_" + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "dash")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "-" + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "-" + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + "-" + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "nothing")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + f.Substring(intFileSourceLength + 1, f.Length - intFileSourceLength - 1));
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                MessageBox.Show(intNumFilesCopied + " files renamed.");
                Close();
            }
            catch (Exception exName)
            {
                MessageBox.Show(exName.Message.ToString());
            }
           
        }//End of MoveFileBefore

       private void MoveFileBeforeNewName()
        {
            try
            {
                int intFileSourceLength = txtSourceDirectory.Text.Length;

                int intFileCounter = Convert.ToInt32(txtStartNumbersAt.Text);
                int intNumFilesCopied = 0;
                string foldername = txtSourceDirectory.Text;

                if (cbSeparatedBy.SelectedItem.ToString() == "space")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + " " + txtNewName.Text + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + " " + txtNewName.Text + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + " " + txtNewName.Text + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "underscore")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "_" + txtNewName.Text + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "_" + txtNewName.Text + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + "_" + txtNewName.Text + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "dash")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "-" + txtNewName.Text + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + "-" + txtNewName.Text + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + "-" + txtNewName.Text + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "nothing")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + intFileCounter + txtNewName.Text + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + intFileCounter + txtNewName.Text + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + intFileCounter + txtNewName.Text + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                MessageBox.Show(intNumFilesCopied + " files renamed.");
                Close();
            }
            catch (Exception exName)
            {
                MessageBox.Show(exName.Message.ToString());
            }

        }//End of MoveFileBeforeNewName

        private void MoveFileAfterNewName()
        {
            try
            {
                int intFileSourceLength = txtSourceDirectory.Text.Length;

                int intFileCounter = Convert.ToInt32(txtStartNumbersAt.Text);
                int intNumFilesCopied = 0;
                string foldername = txtSourceDirectory.Text;

                if (cbSeparatedBy.SelectedItem.ToString() == "space")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + " " + intFileCounter + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + " " + intFileCounter + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + txtNewName.Text + " " + intFileCounter + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "underscore")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + "_" + intFileCounter + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + "_" + intFileCounter + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + txtNewName.Text + "_" + intFileCounter + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "dash")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + "-" + intFileCounter + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + "-" + intFileCounter + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + txtNewName.Text + "-" + intFileCounter + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                else if (cbSeparatedBy.SelectedItem.ToString() == "nothing")
                {
                    foreach (string f in Directory.GetFiles(foldername))
                    {
                        pbFileRename.Value++;
                        String strFileExt = GetFileExtension(f);
                        if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        {
                            if (chkSame.Checked == true || txtSourceDirectory.Text == txtDestinationDirectory.Text)
                            {
                                File.Move(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + intFileCounter + strFileExt);
                            }
                            else
                            {
                                File.Copy(f, txtDestinationDirectory.Text + "\\" + txtNewName.Text + intFileCounter + strFileExt);
                            }
                            //MessageBox.Show(f + " , " + txtDestinationDirectory.Text + "\\" + txtNewName.Text + intFileCounter + strFileExt);
                            intFileCounter++;
                            intNumFilesCopied++;
                        }
                    }
                }
                MessageBox.Show(intNumFilesCopied + " files renamed.");
                Close();
            }

            catch (Exception exName)
            {
                MessageBox.Show(exName.Message.ToString());
                
            }

        }//End of Move File After New Name

        private void txtStartNumbersAt_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }

        }//End allows only numbers to be entered

        private void txtStartNumbersAt_KeyDown(object sender, KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }
            }
        }//Allows only number to be enter for counters

        private string GetFileExtension(string fileName)
        {
            if (fileName.Substring(fileName.Length - 4).ToUpper() == ".JPG")
            {
                return ".JPG";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".GIF")
            {
                return ".GIF";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".PNG")
            {
                return ".PNG";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".BMP")
            {
                return ".BMP";
            }
            else if (fileName.Substring(fileName.Length - 5).ToUpper() == ".JPEG")
            {
                return ".JPEG";
            }
            else if (fileName.Substring(fileName.Length - 5).ToUpper() == ".TIFF")
            {
                return ".TIFF";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".TIF")
            {
                return ".TIF";
            }
            else
            {
                return "";
            }
        }// End of GetFileExtension
    }   
}